﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DotSpatial.Controls ;
using DotSpatial.Data;

using DotSpatial.Symbology;

using DotSpatial.Topology;

using Point = System.Drawing.Point;



namespace HaGisPack
{
    class MoveShapeMF : FMapFunctionBase  
    {

        # region 构造函数据

        public MoveShapeMF() 
        {
            Configure();
        }
        public MoveShapeMF(IMap map)
            : base(map)
        {
            Configure();
        }

        public void Configure()
        {
            _layer = new List<IFeatureLayer>();
        }


        # endregion


        # region 字段

        private List<IFeatureLayer> _layer;

        private bool _dragging;
        private Point _dragposition;
        private Point _mouseposition;
       

       
      
        # endregion 


        # region 属性

        public List<IFeatureLayer> Layer
        {
            get { return _layer; }
            set { _layer = value; }
        }

        # endregion


        # region 方法


        protected override void OnActivate()
        {
            base.OnActivate();
        }

        protected override void OnDeactivate()
        {
            base.OnDeactivate();
        }


        protected override void OnDraw(MapDrawArgs e)
        {

            if (_dragging == false) return; //不在移动时，返回
            if (this._dragging == false) return;

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            GraphicsPath gp = new GraphicsPath();
            Pen Blue = new Pen(Color.Blue, 2);
           
            foreach (IFeatureLayer fl in _layer)  //每个图层
            {
               
                foreach (Feature f in fl.Selection.ToFeatureList())  //每个图层的每个Shape(Feature)
                {
                    List<Point> pts = new List<Point>();

                    foreach (Coordinate c in f.Coordinates)
                    {
                        Point pt = new Point();
                        pt.X = Map.ProjToPixel(c).X - _dragposition.X + _mouseposition.X;
                        pt.Y = Map.ProjToPixel(c).Y + _mouseposition.Y - _dragposition.Y;
                        pts.Add(pt);
                    }   

                    if (fl.DataSet.FeatureType == FeatureType.Point)
                    {
                        gp.AddRectangle(new Rectangle(pts[0].X - 1, pts[0].Y - 1, 2, 2));
                    }                               

                    if (fl.DataSet.FeatureType == FeatureType.Line)
                    {
                        //e.Graphics.DrawLines(Blue, pts.ToArray());

                        GraphicsPath gp1 = new GraphicsPath();
                        gp1.AddLines(pts.ToArray());//先将直线填进一个GraphicsPath
                        gp.AddPath(gp1, false);   //再将这个GraphicsPath添加到另一个GraphicsPath，false表示两个直线之间没有关系
                    }
                    if (fl.DataSet.FeatureType == FeatureType.Polygon)
                    {
                        gp.AddPolygon(pts.ToArray());               
                    }
                     
                }
                //Feature更新
                //fl.DataSet.InitializeVertices();
                //fl.DataSet.UpdateEnvelopes();
            }

            e.Graphics.DrawPath(Blue, gp);

            Blue.Dispose();
            gp.Dispose();

            //map更新
            Map.Invalidate();
            Map.MapFrame.Initialize();                      
            
            base.OnDraw(e);
        }

        protected override void OnMouseDown(GeoMouseArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _dragging = true;
                _dragposition = e.Location;
            }
           
            
            base.OnMouseDown(e);
        }
        
        protected override void OnMouseMove(GeoMouseArgs e)
        {
            _mouseposition = e.Location;

            if (_dragging == false) return;



            Map.Invalidate();
            Map.MapFrame.Initialize();
            base.OnMouseMove(e);
        }


        protected override void OnMouseUp(GeoMouseArgs e)
        {
            if (_dragging == false) return;

            foreach (IFeatureLayer fl in _layer)
            {
                foreach (Feature f in fl.Selection.ToFeatureList())
                {
                    foreach (Coordinate c in f.Coordinates)
                    {
                        c.X = c.X + Map.PixelToProj(_mouseposition).X - Map.PixelToProj(_dragposition).X;
                        c.Y = c.Y + Map.PixelToProj(_mouseposition).Y - Map.PixelToProj(_dragposition).Y;
                    }
                
                }
 
                //Feature更新
                fl.DataSet.InitializeVertices();
                fl.DataSet.UpdateExtent();
                        
            }


            _dragging = false;

            //map更新（即表现更新）
            Map.Invalidate();
            Map.MapFrame.Initialize();

            base.OnMouseUp(e);
        }




        # endregion

        
    }
}
